<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *项目管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择信息！','error');
	}
}
$return['sex']=$this->sex2;
$return['pay']=$this->xinzhis;
$return['job_type']=$this->job_type;
$return['job_paytype']=$this->job_paytype;
$return['job_baotype']=$this->job_baotype;
if($_RQ['op']=='list'){
	$params=array('merchid'=>$merchid,'type'=>0);
	$params['uniacid']=$_SESSION['uniacid'];
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$return['params']['status']=intval($_RQ['status']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_51job_job',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_51job/item_detail//id='.$item['id'],true);
			$merch=pdo_get('jy_51job_merch',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
			$item['merch']=$merch;
			$item['jobname']=pdo_getcolumn('jy_51job_job_group',array('id'=>$item['cid'],'uniacid'=>$_SESSION['uniacid']),'title');
			$item['onlinetime']=json_decode($item['onlinetime'],true);
			$item['baotypetitle']=$this->job_paytype2[$item['paytype']].'结';
			if($item['istop'] && $item['toptime']<time()){
				pdo_update('jy_51job_job',array('istop'=>0),array('id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['istop']=0;
			}
			if($item['istop']){
				$item['toptime']=date('Y-m-d H:i:s',$item['toptime']);
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'应聘信息列表'),
			array('op'=>'edit','title'=>'添加应聘信息'),
		),
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：这里显示企业发布的应聘</red>','edit'=>true,'list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'merch.title','title'=>'公司名称','type'=>'string'),
			array('type'=>'string','title'=>'应聘岗位','name'=>'jobname'),
			array('name'=>'pay','title'=>'薪资','type'=>'string'),
			array('type'=>'radio','title'=>'应聘类型','name'=>'type','help'=>'','options'=>'job_type'),
			array('type'=>'radio','title'=>'薪资类型','name'=>'paytype','help'=>'','options'=>'job_paytype'),
			array('name'=>'sex','title'=>'性别要求','type'=>'radio','options'=>'sex'),
			array('name'=>'looks','title'=>'浏览数量','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'ishot','title'=>'是否置顶','type'=>'switch','set'=>'set'),
		)),
		'search'=>array(
			array('name'=>'mid','width'=>15,'title'=>'发布人','type'=>'choose','action'=>'core/member.get_member/'),
			//array('name'=>'status','width'=>15,'title'=>'是否启用','type'=>'radio','width'=>'15','options'=>'job_status'),
		)
	);
	$return['sex']=$this->sex2;
	$return['job_status']=$this->job_status;
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$merch=pdo_get('jy_51job_merch',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$merch['istop_day']=pdo_count('jy_51job_toplog',array('ispay'=>0,'status'=>1,'createtime >'=>$daytime,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$lasttop_day=$merch['top_day']-$merch['istop_day'];
	$lasttop_day=$lasttop_day>0?$lasttop_day:0;
	$jobtopfee=$lasttop_day<=0?$return['pluginset']['jobtopfee']*1:0;
	$return['post']=array('day'=>1,'fee'=>$jobtopfee,'lasttop_day'=>$lasttop_day);
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_51job_job',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['images']=json_decode($item['images'],true);
		$item['lat_lng']=json_decode($item['lat_lng'],true);
		$item['onlinetime']=json_decode($item['onlinetime'],true);
	}else{
		$item=array();
		$item['jobuptime']=$this->model('merch')->jobuptime($merchid);
	}
	$item['pcd']=array('province'=>$item['province'],'city'=>$item['city'],'district'=>$item['district']);
	$item['onlinetime']=$item['onlinetime']?json_decode($item['onlinetime'],true):array('start'=>'09:00','end'=>'18:00');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回项目列表'),
		),
		'edit'=>array(
					array('type'=>'string','title'=>'应聘名称','name'=>'title'),
					array('type'=>'select2','title'=>'应聘岗位','name'=>'pid','options'=>'pids','title2'=>'岗位2级分类','name2'=>'cid','options2'=>'cids'),
					array('type'=>'radio','title'=>'性别要求','name'=>'sex','help'=>'','options'=>'sex'),
					array('type'=>'string','title'=>'年龄要求','name'=>'ageinfo','help'=>'文字描述：例如：22岁~30岁'),
					array('type'=>'radio','title'=>'学历要求','name'=>'xueli','options'=>'xueli_type'),
					array('type'=>'richtext','title'=>'应聘详情','name'=>'remark','help'=>''),
					array('type'=>'richtext','title'=>'福利待遇','name'=>'fuli','help'=>''),
					array('type'=>'radio','title'=>'应聘类型','name'=>'type','help'=>'','options'=>'job_type'),
					array('type'=>'radio','title'=>'薪资类型','name'=>'paytype','help'=>'','options'=>'job_paytype'),
					array('type'=>'unitstring','title'=>'薪资','name'=>'price','unit'=>'/月','show'=>array('paytype'=>0)),
					array('type'=>'unitstring','title'=>'薪资','name'=>'price','unit'=>'/天','show'=>array('paytype'=>1)),
					array('type'=>'unitstring','title'=>'薪资','name'=>'price','unit'=>'/小时','show'=>array('paytype'=>2)),
					array('type'=>'unitstring','title'=>'薪资','name'=>'price','unit'=>'/次','show'=>array('paytype'=>3)),
					array('name'=>'onlinetime','title'=>'上班时间','type'=>'time_hour'),
					//array('type'=>'radio','title'=>'保险类型','name'=>'baotype','help'=>'','options'=>'job_baotype'),
					//array('type'=>'pcd','title'=>'地址','name'=>'pcd'),
					//array('type'=>'string','title'=>'地址','name'=>'address'),
					array('type'=>'lat_lng','title'=>'坐标','name'=>'lat_lng'),
					array('type'=>'images','title'=>'多图','name'=>'images','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>'设置是否启用'),
					array('name'=>'paixu','title'=>'排序','type'=>'string'),
		)
	);
	$return['pids']=pdo_getall('jy_51job_job_group',array('pid'=>0,'uniacid'=>$_SESSION['uniacid']),array('id','title'));
	foreach($return['pids'] as $k=>&$pv){
		$pv['cids']=pdo_getall('jy_51job_job_group',array('pid'=>$pv['id'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
		if($item['pid']==$pv['id']){
			$item['pidindex']=$k;
		}
	}
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$return['item']=$item;
	$return['xueli_type']=$this->xueli_type;
	foreach($return['xueli_type'] as &$xl){
		$xl=$xl.'以上';
	}
	$return['xueli_type'][0]='学历不限';
	$return['time_hour']=array('00:00','01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00','24:00');
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['title']){
		exi('应聘名称不能为空！','error');
	}
	if(!$postdata['images']){
			//exi('图片不能为空！','error');
	}
	if(!$postdata['merchid']){
		//exi('发布企业不能为空！','error');
	}
	if(empty($postdata['lat_lng'])){
			//exi('请选择坐标！','error');
	}
	/*if($postdata['pcd']){
		if($postdata['pcd']['city']){
			if($postdata['pcd']['city']=='北京市' || $postdata['pcd']['city']=='上海市' || $postdata['pcd']['city']=='天津市'){
				$address=$postdata['pcd']['city'].' 市辖区';
			}else{
				$address=$postdata['pcd']['province'].' '.$postdata['pcd']['city'];
			}
			$code=pdo_getcolumn('core_area',array('address'=>$address),'parent_code');
		}
	}*/
	$merch=pdo_get('jy_51job_merch',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	$postdata['pcd']=json_decode($merch['pcd'],true);
	$jobname=pdo_getcolumn('jy_51job_job_group',array('id'=>$item['cid'],'uniacid'=>$_SESSION['uniacid']),'title');
	$company_title=pdo_getcolumn('jy_51job_merch',array('id'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'title');
	if(!$postdata['price']){
		$pay='面议';
	}else{
		$postdata['paytype']=$postdata['paytype']?:0;
		$pay=$postdata['price'].'元/'.$this->job_paytype2[$postdata['paytype']];
	}
	$data=array(
		'title'=>trim($postdata['title'])?:$company_title.'应聘['.$jobname.']',
		'images'=>ijson_encode($postdata['images']),
		'pay'=>$pay,
		'remark'=>trim($postdata['remark']),
		'fuli'=>trim($postdata['fuli']),
		'name'=>trim($postdata['name']),
		'mobile'=>trim($postdata['mobile']),
		'address'=>trim($postdata['address']),
		'status'=>intval($postdata['status']),
		'sex'=>intval($postdata['sex']),
		'province'=>$postdata['pcd']['province'],
		'city'=>$postdata['pcd']['city'],
		'district'=>$postdata['pcd']['district'],
		'code'=>$code,
		'looks'=>intval($postdata['looks']),
		'pid'=>intval($postdata['pid']),
		'cid'=>intval($postdata['cid']),
		'lat_lng'=>ijson_encode($postdata['lat_lng']),
		'onlinetime'=>ijson_encode($postdata['onlinetime']),
		'price'=>trim($postdata['price']),
		'paytype'=>trim($postdata['paytype']),
		'baotype'=>trim($postdata['baotype']),
		'paixu'=>intval($postdata['paixu']),
		'ageinfo'=>trim($postdata['ageinfo']),
		'xueli'=>intval($postdata['xueli']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_51job_job',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['type']=0;
		$data['merchid']=$merchid;
		$data['createtime']=time();
		pdo_insert('jy_51job_job',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_51job_job',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的项目！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_51job_job',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['ishot'])){
		$basic=$this->plugin_setting('basic');
		$topdays=intval($basic['topdays'])?:7;
		$topendtime=TIMESTAMP+$topdays*24*3600;
		pdo_update('jy_51job_job',array('ishot'=>intval($post['ishot']),'topendtime'=>$topendtime),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='buytop'){
	$post=get_postdata();
	$jobid=intval($post['jobid']);
	$day=intval($post['day']);
	if(!$jobid){
		exi('请先选择职位！','error');
	}
	if($day<=0){
		exi('请先填写置顶天数！','error');
	}
	if($post['lasttop_day']>0 && $post['lasttop_day']>=$day){
		$jobtopfee=0;
	}else{
		$jobtopfee=$return['pluginset']['jobtopfee']*$day;
	}
	if($jobtopfee>0){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'merchid'=>$merchid,
			'jobid'=>$jobid,
			'ordersn'=>get_ordersn('jy_51job_toplog','ordersn'),
			'fee'=>$jobtopfee,
			'day'=>$day,
			'ispay'=>0,
			'status'=>0,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_51job_toplog',$insert);
		$id=pdo_insertid();
		$params['fee']=$jobtopfee;
		$params['title']='购买职位置顶';
		$params['id']=$id;
		$params['plugin']='jy_51job';
		$params['back_url']='jy_51job/merch.zhao/payresult/';
		$return['payid']=cfc('pay')->create_pay_order($params);	
		pdo_update('jy_51job_toplog',array('pay_id'=>$return['payid']),array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
		exi($return);
	}else{
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'merchid'=>$merchid,
			'jobid'=>$jobid,
			'ordersn'=>get_ordersn('jy_51job_toplog','ordersn'),
			'day'=>$day,
			'ispay'=>0,
			'status'=>1,
			'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_51job_toplog',$insert);
		$istoptime=pdo_getcolumn('jy_51job_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$jobid),'toptime');
		$istoptime=$istoptime>TIMESTAMP?$istoptime:TIMESTAMP;
		$toptime=$istoptime+$day*24*3600;
		pdo_update('jy_51job_job',array('toptime'=>$toptime,'istop'=>1),array('id'=>$jobid,'uniacid'=>$_SESSION['uniacid']));
		exi('置顶成功','success','jy_51job/merch.zhao/');
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_51job_toplog',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！购买会员订单不存在！','error');
	}
	if($order['fee']!=$payresult['fee']){
		exi('支付失败！购买会员订单金额不匹配！','error');
	}
	pdo_update('jy_51job_toplog',array('status'=>1,'ispay'=>1),array('id'=>$order['id'],'uniacid'=>$_SESSION['uniacid']));
	$istoptime=pdo_getcolumn('jy_51job_job',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['jobid']),'toptime');
	$istoptime=$istoptime>TIMESTAMP?$istoptime:TIMESTAMP;
	$toptime=$istoptime+$order['day']*24*3600;
	pdo_update('jy_51job_job',array('toptime'=>$toptime,'istop'=>1),array('id'=>$order['jobid'],'uniacid'=>$_SESSION['uniacid']));
	exi('支付成功','success','jy_51job/merch.index/');
}